<?php
/**
 * @Author:      lyt
 * @DateTime:  2017-05-24 14:39:10
 * @Description: 脚本清理指定用户的订单账务
 */
$api_service_path = __DIR__ . "/../../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();
DB::connection()->disableQueryLog(); 

$temp=array(15114829243718,15114817822861,15114843266850,15114836829999,15114836361076);
//循环处理订单
foreach($temp as $w){
	$wr=WposRecord::find($w); 
	if($wr){
		$p_r=Pos::statusOrder($wr->id);
		if($p_r->status==0&&$p_r->data->trade_status=='PAY'){
			$wr->cashier_trade_no=$p_r->data->cashier_trade_no;
            $wr->mcode=$p_r->data->mcode;
            $wr->device_en=$p_r->data->device_en;
            $wr->trade_status=$p_r->data->trade_status;
            $wr->pay_info=$p_r->data->pay_info;
            $wr->attach=$p_r->data->attach;
            $wr->pay_type=$p_r->data->pay_type;
            $wr->pay_fee=$p_r->data->pay_fee;
            $wr->check_fee=$p_r->data->check_fee;
            $wr->thirdDiscount=$p_r->data->thirdDiscount;
            $wr->thirdMDiscount=$p_r->data->thirdMDiscount;
            $wr->time_end=$p_r->data->time_end;
            $wr->buyer=isset($p_r->data->buyer)?$p_r->data->buyer:'';
            $wr->thirdSerialNo=isset($p_r->data->thirdSerialNo)?$p_r->data->thirdSerialNo:'';
            $wr->terminalNo=isset($p_r->data->terminalNo)?$p_r->data->terminalNo:'';
            $wr->vouchNo=isset($p_r->data->vouchNo)?$p_r->data->vouchNo:'';
            $wr->batchNo=isset($p_r->data->batchNo)?$p_r->data->batchNo:'';
            $wr->cardType=isset($p_r->data->cardType)?$p_r->data->batchNo:'';
            $wr->implement_time=Common::currentTimeString();
            $wr->save();           
            $pay_type=WposRecord::$pay_type[$p_r->data->pay_type];
            switch ($wr->type) {
	        	case 1://充值
	        		$tran_no = date('YmdHis',time()).Common::generateCode(8);   
	        		$aur=AcountUserRecharge::find($wr->buss_id);
	        		if($aur->status!=5){
	        			echo " 充值记录 ：".$wr->buss_id." 状态错误";
						Log::info(" 充值记录 ：".$wr->buss_id." 状态错误");
						break;
	        		}
	        		AcountUserRecharge::where("id",$wr->buss_id)->update(array("status"=>1,"implement_time"=>Common::currentTimeString(),"tran_no"=>$tran_no,"pay_type"=>$pay_type,"pay_id"=>$wr->id));
	        		$aur=AcountUserRecharge::find($wr->buss_id);
	        		Account::record($aur->user_id,$aur->shop_id,1,$tran_no,1,121,$aur->amount,'pos充值收入,充值记录:'.$aur->id);				    
	        		break;
	        	case 2://订单支付 处理订单逻辑
	        		$tran_no = date('YmdHis',time()).Common::generateCode(8);   
	        		$op=OrderPay::find($wr->buss_id);
	        		$order = Orders::where("order_id",$op->order_id)->first();
	        		if($op->status!=5){
	        			echo " 订单支付记录 ：".$wr->buss_id." 状态错误";
						Log::info(" 订单支付记录 ：".$wr->buss_id." 状态错误");
						break;
	        		}

	        		OrderPay::where("id",$wr->buss_id)->update(array("status"=>1,"implement_time"=>Common::currentTimeString(),"tran_no"=>$tran_no,"pay_type"=>$pay_type,"pay_id"=>$wr->id));
	        			//记录用户收入
	        		Account::record($order->pay_user_id,$order->shop_id,1,$tran_no,1,102,$op->amount,'订单结算充值,订单号:'.$order->order_id);
		            //记录用户支出
		            Account::record($order->pay_user_id,$order->shop_id,1,$tran_no,2,202,$op->amount,'订单结算支出,订单号:'.$order->order_id);
		            //记录公司账户收入
		            Account::record_base('1',$tran_no,1,102,$op->amount,'订单结算收入,订单号:'.$order->order_id);
		            //记录订单账务关系
		            OrderAccount::insert(array('order_id' => $order->order_id,'tran_no' => $tran_no,));

	        		$op_arr=OrderPay::where("order_id",$order->order_id)->whereIn("status",array(0,1,5))->get();
			        $order->payable_amount=$order->payable_amount+$op->amount;
			        $order->pay_type=sizeof($op_arr)==1?$pay_type:9;
			        // $order->pay_time=date("Y-m-d H:i:s",time()+Config::get("config.report_time_diff"));
			        $order->save();
				    
	        		break;
	        	case 3://洗车单支付
	        		$tran_no = date('YmdHis',time()).Common::generateCode(8);
                    $cwopr=CarWashOrderPayRecord::find($wr->buss_id); 
                    $cwo=CarWashOrder::find($cwopr->order_id);                             
                    if($cwopr->status!=5){
	        			echo " 洗车单支付记录 ：".$wr->buss_id." 状态错误";
						Log::info(" 洗车单支付记录 ：".$wr->buss_id." 状态错误");
						break;
	        		}
                   
                    CarWashOrderPayRecord::where("id",$wr->buss_id)->update(array("status"=>1,"implement_time"=>Common::currentTimeString(),"tran_no"=>$tran_no,"pay_type"=>$pay_type,"pay_id"=>$wr->id)); 
                    	//记录用户充值 不记录充值记录  只记录流水
                    Account::record($cwo->user_id,$cwo->shop_id,1,$tran_no,1,106,$cwopr->amount,'洗车单结算充值,洗车单号:'.$cwo->order_id);
                    //记录用户支出
                    Account::record($cwo->user_id,$cwo->shop_id,1,$tran_no,2,206,$cwopr->amount,'洗车单结算支出,洗车单号:'.$cwo->order_id);
                    //记录公司账户收入
                    Account::record_base('1',$tran_no,1,106,$cwopr->amount,'洗车单结算收入,洗车单号:'.$cwo->order_id);
                    //记录订单账务关系
                    CarWashOrderAccount::insert(array('order_id' => $cwo->order_id,'tran_no' => $tran_no,));

                	$cwo->pay_type=$pay_type;
			        $cwo->pay_time=date("Y-m-d H:i:s",time()+Config::get("config.report_time_diff"));
			        $cwo->status=1417;
			        $cwo->save();

			        $order_flows = new CarWashOrderFlows;
                    $order_flows->order_id = $cwo->order_id;
                    $order_flows->status =1417;
                    $order_flows->user_id = 0;
                    $order_flows->type = 'sc';
                    $order_flows->save();
                                        
	        		break;
	        	case 4://收回单
	        		$tran_no = date('YmdHis',time()).Common::generateCode(8);   
	        		$op=ReceivablePay::find($wr->buss_id);
	        		$receivable = Receivable::find($op->receivable_id);
	        		if($op->status!=5){
	        			echo " 收回单支付记录 ：".$wr->buss_id." 状态错误";
						Log::info(" 收回单支付记录 ：".$wr->buss_id." 状态错误");
						break;
	        		}
	        		ReceivablePay::where("id",$wr->buss_id)->update(array("status"=>1,"implement_time"=>Common::currentTimeString(),"tran_no"=>$tran_no,"pay_type"=>$pay_type,"pay_id"=>$wr->id));	        		
        			if($receivable->type==2){//如果是起初回收
        				$ba=BeginArrear::where("shop_id",$receivable->shop_id)->where("company_id",$receivable->company_id)->lockForUpdate()->first();
        				Account::record($receivable->company_id,0,3,$tran_no,1,118,$op->amount,'期初收回收入,收回单号:'.$receivable->receivable_id);
		                //记录用户收入
		                Account::record_base('1',$tran_no,1,118,$op->amount,'期初收回收入,收回单号:'.$receivable->receivable_id);
		                $ba->current_amount=$ba->current_amount-$op->amount;
						$ba->save();
        			}else{//如果是订单回收
        				//记录用户充值 不记录充值记录  只记录流水
		                Account::record($receivable->company_id,0,3,$tran_no,1,114,$op->amount,'订单收回收入,收回单号:'.$receivable->receivable_id);
		                //记录用户支出
		                Account::record_base('1',$tran_no,1,114,$op->amount,'订单收回收入,收回单号:'.$receivable->receivable_id);
        			}
        			$op_arr=ReceivablePay::where("receivable_id",$receivable->receivable_id)->whereIn("status",array(0,1,5))->get();
			        $receivable->pay_type=sizeof($op_arr)==1?$pay_type:9;
			        $receivable->save();
				   
	        		break;
	        	case 5://套餐卡订单支付
	        		$tran_no = date('YmdHis',time()).Common::generateCode(8);   
	        		$op=ServiceCardOrderPayRecord::find($wr->buss_id);
	        		$order = ServiceCardOrder::where("order_id",$op->order_id)->first();
	        		if($op->status!=5){
	        			echo " 套餐卡订单支付记录 ：".$wr->buss_id." 状态错误";
						Log::info(" 套餐卡订单支付记录 ：".$wr->buss_id." 状态错误");
						break;
	        		}
	        		ServiceCardOrderPayRecord::where("id",$wr->buss_id)->update(array("status"=>1,"implement_time"=>Common::currentTimeString(),"tran_no"=>$tran_no,"pay_type"=>$pay_type,"pay_id"=>$wr->id));
	        			//记录用户收入
	        		Account::record($order->user_id,$order->shop_id,1,$tran_no,1,123,$op->amount,'套餐卡订单结算充值,订单号:'.$order->order_id);
		            //记录用户支出
		            Account::record($order->user_id,$order->shop_id,1,$tran_no,2,223,$op->amount,'套餐卡订单结算支出,订单号:'.$order->order_id);
		            //记录公司账户收入
		            Account::record_base('1',$tran_no,1,123,$op->amount,'套餐卡订单结算收入,订单号:'.$order->order_id);
		            //记录订单账务关系
		            ServiceCardOrderAccount::insert(array('order_id' => $order->order_id,'tran_no' => $tran_no,));

	        		$op_arr=ServiceCardOrderPayRecord::where("order_id",$order->order_id)->whereIn("status",array(0,1,5))->get();
			        $order->pay_amount=$order->pay_amount+$op->amount;
			        $order->pay_type=sizeof($op_arr)==1?$pay_type:9;
			        $order->save(); 
	        		break;
		    }
		    echo " wpos 查询记录 ：".$w." 处理完成";
			Log::info(" wpos 查询记录 ：".$w." 处理完成");
		}else{
			echo " wpos 查询记录 ：".$w." 不存在";
			Log::info(" wpos 查询记录 ：".$w." 不存在");
		}
	}else{
		echo " wpos 记录 ：".$w." 不存在";
		Log::info(" wpos 记录 ：".$w." 不存在");
	}      
}





